package net.mudfox.stage;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import net.mudfox.base.BaseRestActivity;
import net.mudfox.factory.Entry;
import net.mudfox.factory.ID;
import net.mudfox.factory.Url;
import net.mudfox.helper.AdapterHelper;
import net.mudfox.helper.CommonHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends BaseRestActivity {

    ListView listView;
    SwipeRefreshLayout swipeLayout;
    JSONArray array;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        actionBar.setDisplayHomeAsUpEnabled(false);

        entry = Entry.AssembleFlow;
        baseUrl = Url.AssembleFlow;

        listView = findViewById(R.id.listView);
        swipeLayout = findViewById(R.id.swipeLayout);
        registerForContextMenu(listView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, ID.ACTION_ADD, 0, "添加...");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == ID.ACTION_ADD)
            startActivityForResult(new Intent(this,
                            AssembleFlowEditor.class),
                    ID.ASSEMBLE_FLOW_EDITOR);
        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        // getMenuInflater().inflate(R.menu.operate_menu, menu);
        menu.add(0, ID.ACTION_COPY, 0, "复制");
        menu.add(0, ID.ACTION_COPY_ADD, 1, "复制新增...");
        menu.add(0, ID.ACTION_EDIT, 2, "编辑...");
        menu.add(0, ID.ACTION_REMOVE, 3, "删除");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if (array.isEmpty())
            return false;

        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int position = info.position;
        JSONObject object = array.getJSONObject(position);
        if (object.isEmpty())
            return false;

        switch (item.getItemId()) {
            case ID.ACTION_COPY:
                return do_copyItem(object);
            case ID.ACTION_COPY_ADD:
                return do_copyAddItem(object);
            case ID.ACTION_EDIT:
                return do_editItem(object);
            case ID.ACTION_REMOVE:
                return do_removeItem(object);
            default:
                return super.onContextItemSelected(item);
        }
    }

    boolean do_copyItem(JSONObject object) {
        String detail = object.getString("detail");
        if (detail.isEmpty())
            return false;

        CommonHelper.setClipboardText(this, entry, detail);
        CommonHelper.showMessage(this, "已复制到剪贴板");
        return true;
    }

    boolean do_copyAddItem(JSONObject object) {
        return true;
    }

    boolean do_editItem(JSONObject object) {
        int id = object.getIntValue("id");
        String type = object.getString("type_");
        String partner = object.getString("partner");
        String name = object.getString("name");
        int number = object.getIntValue("number");
        String remark = object.getString("remark");

        if (id == 0 || type.isEmpty() ||
                partner.isEmpty() || name.isEmpty() ||
                number == 0)
            return false;

        Intent intent = new Intent(this, AssembleFlowEditor.class);
        intent.putExtra("id", id);
        intent.putExtra("type_", type);
        intent.putExtra("partner", partner);
        intent.putExtra("name", name);
        intent.putExtra("number", number);
        intent.putExtra("remark", remark);
        startActivityForResult(intent, ID.ASSEMBLE_FLOW_EDITOR);
        return true;
    }

    boolean do_removeItem(JSONObject object) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确定操作").setMessage("确定删除吗？");
        builder.setPositiveButton(R.string.sure,
                (paramAnonymousDialogInterface, paramAnonymousInt) ->
                {
                    int id = object.getIntValue("id");
                    if (id > 0)
                        httpGet(baseUrl + "_remove/" + id);
                });

        builder.setNegativeButton(R.string.cancel,
                (paramAnonymousDialogInterface, paramAnonymousInt) -> {
                });

        builder.create().show();
        return true;
    }

    @Override
    public void processReply(JSONObject rootObject) {
        array = AdapterHelper.verifyObject(rootObject, entry);
        if (array.isEmpty()) {
            listView.setAdapter(null);
            return;
        }

        List<Map<String, Object>> model = new ArrayList<>();
        for (int i = 0; i < array.size(); i++) {
            JSONObject object = array.getJSONObject(i);
            Map<String, Object> item = new HashMap<>();
            item.put("date_", object.getString("date_"));
            String detail = object.getString("type_") + "  " +
                    object.getString("partner") + "  " +
                    object.getString("name") + "  " +
                    object.getInteger("number") + "  " +
                    object.getString("remark");
            item.put("detail", detail);
            model.add(item);
        }

        SimpleAdapter adapter = AdapterHelper.getTableAdapter(this, model,
                new String[]{"date_", "detail"});
        listView.setAdapter(adapter);
    }
}
